
$(function() {
    var name = $("#name"),
            email = $("#email"),
            password = $("#password"),
            allFields = $([]).add(name).add(email).add(password),
            tips = $(".validateTips");

    function updateTips(t) {
        tips.text(t)
            .addClass("ui-state-highlight");

        setTimeout(function() {
            tips.removeClass("ui-state-highlight", 1500);
        }, 500);

    }



    function checkLength(o, n, min, max) {
        if (o.val().length > max || o.val().length < min) {
            o.addClass("ui-state-error");
            updateTips("Length of " + n + " must be between " +
                    min + " and " + max + ".");
            return false;
        } else {
            return true;
        }
    }



    function checkRegexp(o, regexp, n) {
        if (!(regexp.test(o.val()))) {
            o.addClass("ui-state-error");
            updateTips(n);
            return false;
        } else {
            return true;
        }
    }



    $("#login-form").dialog({
        autoOpen: false,
        height: 250,
        width: 500,
        modal: true,
        buttons: {
            "登入": function() {

            	$.ajax({
                    url: "ILS_login.ils?ils=login",
                    async: false,
                    data:getLoginData(),
                    type: 'post',
                    success: function(res) {
                    	if("success" == res){
                            alert("歡迎!");
                            $("#login-form").dialog("close");
                    	}else{
                    		alert("帳號或密碼不正確!");
                    	}
                    },
                    error: function(data) {
                        alert("連線失敗！");
                    }
                });
            },
            "取消": function() {
                $(this).dialog("close");
            }
        },
        close: function() {
            allFields.val("").removeClass("ui-state-error");
        }
    });
    $("#openLogin").click(function() {
    $("#login-form").dialog("open");
    });
});

//得到目前所有資料
function getLoginData(){
    var dataObject = {
        "loginAccount" : $("#loginAccount").val(),
        "loginPassword" : $("#loginPassword").val(),
    };
    
    return dataObject;
}